clear
clc

Width=14;           %数据宽度bit
Depth=4096;         %数据深度
Maxi=2^Width-1;     %数据峰值

x = linspace(0, 2 * pi, Depth);
y = sin(x);
y = round(y * (2 ^ (Width - 1) - 1) + 2 ^ (Width - 1) -1);%量化
y=int32(y);
figure(1)
subplot(311)
plot(y);

fileID=fopen("sine_wave.coe");
data1=textscan(fileID,'%d','HeaderLines',2,'Delimiter',',');
data1=data1{1};
data1=data1';
subplot(312)
plot(data1)

subplot(313)
diff1=data1-y;
plot(diff1)

mean(diff1);

fid = fopen('dds_sin.coe','w');
fprintf(fid,'memory_initialization_radix=10;\n');
fprintf(fid,'memory_initialization_vector =\n');
fprintf(fid, '%d,\n', y);
 
fclose(fid);

z=zeros(1,Depth);
z(1:Depth/2)=round(linspace(0,Maxi,Depth/2));
z(Depth/2+1:end)=round(linspace(Maxi,0,Depth/2));
z=int32(z);

fileID=fopen("triangular_wave.coe");
data2=textscan(fileID,'%d','HeaderLines',7,'Delimiter',',');
data2=data2{1};
data2=data2';
figure(2)
subplot(311)
plot(data2)
subplot(312)
plot(z)
subplot(313)
diff2=data2-z;
plot(diff2)

fid = fopen('dds_triangular.coe','w');
fprintf(fid,'memory_initialization_radix=10;\n');
fprintf(fid,'memory_initialization_vector =\n');
fprintf(fid, '%d,\n', z);